/**
 * Created by 白雨浓 on 2018/9/27 14:51.
 */
import chokidar from 'chokidar'


let watcher;
export const fileScan = {
    watch({path, exts}, ready, add) {
        watcher = chokidar.watch(path, {
            persistent: false
        });

        exts = exts.map(ext => '.' + ext);

        watcher.on('ready', () => {
            ready();
            watcher.on('add', path => {
                const res = this.ext(path);
                if (exts.indexOf(res[0]) >= 0) {
                    add(path);
                }
            })
        });
    },
    ext(path) {
        return /\.[^.]+/.exec(path);
    },
    close() {
        watcher.close();
    }
};